<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Cate;
use app\common\model\Supply;

class Index
{
    //程宇
    //获取首页
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取首页banner
            'tag' => $this->tag(),//获取banner图上标签
            'new' => $this->new(),//获取首页当季鲜品
            //'list' => $this->list(),//获取列表

        ]);
    }

    /**
     * 获取首页banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 1])
            ->field('id,title,image')
            ->limit(0, 1)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }

    /**
     * 获取首页banner标签
     */
    public function tag()
    {
        $model = New Cate();
        $type = [1, 2, 3, 7];
        $tag = $model->where('status', 1)
            ->where('type', 'in', $type)
            ->field('id,name,image')
            ->select();
        foreach ($tag as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $tag;
    }

    /**
     * 获取首页当季新品
     */
    public function new()
    {
        $model = New Supply();
        $new = $model->where('status', 1)
            ->field('id,image,title')
            ->order('id', 'desc')
            ->limit(0, 10)
            ->select();
        foreach ($new as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $new;
    }

    /**
     * 获取分类
     */
    public function list()
    {
        $model = New Cate();
        $cate_id = input('id');
        // 先查询出顶级分类，也就是父级id为0的
        $cate = $model->where(['status' => 1, 'pid' => 0, 'type' => 4])
            ->where('status', 1)
            ->limit(0, 6)
            ->field('id,name,image,tag')
            ->select()->toArray();
        foreach ($cate as &$v) {
            $v['image'] = env('app.yuming') . $v['image'];
            unset($v);
        }
        $res = [];
        foreach ($cate as $v) {
            $res[$v['id']] = $v;
            $res[$v['id']]['child'] = Cate::where('pid', $v['id'])
                ->where('status', 1)
                ->limit(0, 8)
                ->field('id,name')
                ->select()->toArray();
            $res = array_values($res);
        }
        foreach ($res as &$v) {
            $ids = array_column($v['child'], 'id');
            if (!empty($cate_id)) {
                if (in_array($cate_id, $ids)) {
                    $cate = Cate::where('pid', $cate_id)->column('id');
                } else {
                    $cate = Cate::where('pid', 'in', $ids)->column('id');
                }
            } else {
                $cate = Cate::where('pid', 'in', $ids)->column('id');
            }
            $v['goods'][] = Supply::where('cate_id', 'in', $cate)
                ->where('status', 1)
                ->limit(0, 8)
                ->field('id,title,image,price,turnover')
                ->select()->toArray();
            foreach ($v['goods'] as &$vv) {
                foreach ($vv as &$vvv) {
                    $vvv['image'] = env('app.yuming') . $vvv['image'];
                }
            }
            unset($v);
        };
        return sucessMsg('200', '获取成功', $res);
    }

}
